package com.mo.lib.utils

import android.content.pm.PackageManager
import com.dylanc.longan.application
import com.dylanc.longan.packageName

/**
 * @ author：mo
 * @ data：2019/2/13:14:18
 * @ 功能：清单文件相关
 */
object Manifests {
    /**获取清单文件中 MetaData 的 String 值 */
    fun getStringMetaData(key: String): String? {
        val metaData = getMetaData(key)
        return if (metaData is String) {
            metaData
        } else null
    }

    /** 获取清单文件中 MetaData 的 int 值*/
    fun getIntMetaData(key: String)= getIntMetaData(key, 0)

    /**获取清单文件中 MetaData 的 int 值 */
    fun getIntMetaData(key: String?, defValue: Int): Int {
        val metaData = getMetaData(key)
        return if (metaData is Int) {
            metaData
        } else defValue
    }

    /** 获取清单文件中 MetaData 的 boolean 值*/
    fun getBooleanMetaData(key: String?): Boolean {
        return getBooleanMetaData(key, false)
    }

    /**获取清单文件中 MetaData 的 boolean 值 */
    fun getBooleanMetaData(key: String?, defValue: Boolean): Boolean {
        val metaData = getMetaData(key)
        return if (metaData is Boolean) {
            metaData
        } else defValue
    }

    /** 获取清单文件中 MetaData 的值*/
    fun getMetaData(key: String?): Any? {
        try {
            val aiApplicationInfo = application.packageManager.getApplicationInfo(
                    packageName, PackageManager.GET_META_DATA)
            if (null != aiApplicationInfo) {
                if (null != aiApplicationInfo.metaData) {
                    return aiApplicationInfo.metaData[key]
                }
            }
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return null
    }
}